package com.lsy.weizhi2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.ActivityListObject;
import com.lsy.weizhi2.JsonObject.MyActivityList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.ActivityListAdapter;
import com.lsy.weizhi2.adapter.ManagerAdapter;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.mybase.MyTitleView;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 嗨哟 on 2017/3/31.
 */
public class MyActivityFragment extends Fragment {
    private Context context;
    private RecyclerView recycle_view;
    private ActivityListAdapter activityListAdapter;
    private List<ActivityListObject.ListBean> list;
    private Gson gson;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.my_weizhi_fragment,container,false);
        recycle_view= (RecyclerView) view.findViewById(R.id.recycle_view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context=getActivity();
        gson=new Gson();
        list=new ArrayList<>();
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle_view.setLayoutManager(manager);
        activityListAdapter=new ActivityListAdapter(context,list);
        recycle_view.setAdapter(activityListAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("MyActivityFragment","onResume");
        PostData();
    }

    private void PostData(){
        MyOkHttpUtils.getInstance(context).doGet(Constants.CollectActivity,null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        if(message!=null){
                            list.clear();
                            MyActivityList myActivityList=gson.fromJson(message,MyActivityList.class);
                            if(myActivityList.getList()!=null){
                                int size=myActivityList.getList().size();
                                for (int i = 0; i < size; i++) {
                                    String json=gson.toJson(myActivityList.getList().get(i).getActivity());
                                    ActivityListObject.ListBean bean=gson.fromJson(json,ActivityListObject.ListBean.class);
                                    list.add(bean);
                                }
                                activityListAdapter.UpData(list);
                            }

                        }

                    }
                });
    }
}
